package br.com.mural.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.mural.server.util.ControleGravaBusca;
import br.com.mural.server.util.ParametrosGravaBusca;

public class ServletGravaBusca extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1648234127303883641L;

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String query = req.getParameter(ParametrosGravaBusca.QUERY.toString());
		
		if(query == null || query.length() <= 0) {
			imprimeErro(resp);
			return;
		}
		
		try {
			ControleGravaBusca.gravaBusca(query);
		} finally {
			ControleGravaBusca.fechaArquivo();
		}

		resp.getOutputStream().println("1");
		resp.getOutputStream().flush();
	}
	
	private void imprimeErro(HttpServletResponse resp) throws IOException {
		
		resp.getOutputStream().println("0");
		resp.getOutputStream().flush();
	}
}
